package com.zzyl.nursing.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.core.domain.entity.SysUser;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.dto.NursingTaskDto;
import com.zzyl.nursing.vo.NursingTaskVo;
import com.zzyl.system.mapper.SysUserMapper;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.dto.NursingTaskCancelDto;
import com.zzyl.nursing.dto.NursingTaskDoDto;
import com.zzyl.nursing.dto.NursingTaskDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingTaskMapper;
import com.zzyl.nursing.domain.NursingTask;
import com.zzyl.nursing.service.INursingTaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 护理任务Service业务层处理
 *
 * @author alexis
 * @date 2024-11-17
 */
@Service
public class NursingTaskServiceImpl extends ServiceImpl<NursingTaskMapper, NursingTask> implements INursingTaskService {
    @Autowired
    private NursingTaskMapper nursingTaskMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询护理任务
     *
     * @param id 护理任务主键
     * @return 护理任务
     */
    @Override
    public NursingTask selectNursingTaskById(Long id) {
        return getById(id);
    }

    /**
     * 查询护理任务列表
     *
     * @param
     * @return 护理任务
     */
    @Override
    public List<NursingTask> selectNursingTaskList(NursingTaskDto nursingTaskDto) {
        String nurseId = nursingTaskDto.getNurseId();
        List<NursingTask> nursingTasks = nursingTaskMapper.selectNursingTaskList(nursingTaskDto);
        return nursingTasks;
    }

    @Override
    public List<NursingTaskVo> selectNursingTaskWithNurseNameList(NursingTaskDto nursingTaskDto) {
        List<NursingTask> nursingTasks1 = selectNursingTaskList(nursingTaskDto);
        List<NursingTaskVo> nursingTasks = new ArrayList<>();
        nursingTasks1.forEach(item -> {
            NursingTaskVo nursingTaskVo = new NursingTaskVo();
            BeanUtil.copyProperties(item, nursingTaskVo);
            nursingTasks.add(nursingTaskVo);
        });
        nursingTasks.forEach(item -> {
            String nursingId = item.getNursingId();
            List<String> collect = Arrays.stream(nursingId.split(",")).collect(Collectors.toList());
            List<String> nursingNickName = new ArrayList<>();
            collect.forEach(id -> {
                String string = sysUserMapper.selectNurseName(id);
                nursingNickName.add(string);
            });
            item.setNursingName(nursingNickName);
        });
        return nursingTasks;
    }

    /**
     * 新增护理任务
     *
     * @param nursingTask 护理任务
     * @return 结果
     */
    @Override
    public int insertNursingTask(NursingTask nursingTask) {
        return save(nursingTask) ? 1 : 0;
    }

    /**
     * 修改护理任务
     *
     * @param nursingTask 护理任务
     * @return 结果
     */
    @Override
    public int updateNursingTask(NursingTask nursingTask) {
        return updateById(nursingTask) ? 1 : 0;
    }

    /**
     * 批量删除护理任务
     *
     * @param ids 需要删除的护理任务主键
     * @return 结果
     */
    @Override
    public int deleteNursingTaskByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理任务信息
     *
     * @param id 护理任务主键
     * @return 结果
     */
    @Override
    public int deleteNursingTaskById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 取消护理任务
     */
    @Override
    public int cancelNursingTask(NursingTaskCancelDto nursingTaskCancelDto) {
        NursingTask nursingTask = this.lambdaQuery()
                .eq(NursingTask::getId, nursingTaskCancelDto.getTaskId())
                .one();
        nursingTask.setCancelReason(nursingTaskCancelDto.getReason());
        nursingTask.setStatus(3);
        return updateById(nursingTask) ? 1 : 0;
    }

    /**
     * 执行护理任务
     */
    @Override
    public int doNursingTask(NursingTaskDoDto nursingTaskDoDto) {
        NursingTask nursingTask = this.lambdaQuery()
                .eq(NursingTask::getId, nursingTaskDoDto.getTaskId())
                .one();
        nursingTask.setEstimatedServerTime(nursingTaskDoDto.getEstimatedServerTime());
        nursingTask.setMark(nursingTaskDoDto.getMark());
        nursingTask.setTaskImage(nursingTaskDoDto.getTaskImage());
        nursingTask.setStatus(2);
        return updateById(nursingTask) ? 1 : 0;
    }

    /**
     * 护理任务改期
     */
    @Override
    public int changeNursingTask(NursingTaskDoDto nursingTaskDoDto) {
        NursingTask nursingTask = this.lambdaQuery()
                .eq(NursingTask::getId, nursingTaskDoDto.getTaskId())
                .one();
        nursingTask.setEstimatedServerTime(nursingTaskDoDto.getEstimatedServerTime());
        return updateById(nursingTask) ? 1 : 0;
    }

    /**
     * 查询护理任务列表
     */
    @Override
    public TableDataInfo<NursingTask> queryNursingTaskList(NursingTaskDto nursingTaskDto) {
        LambdaQueryWrapper<NursingTask> queryWrapper = new LambdaQueryWrapper<>();
        //创建分页对象
        Page<NursingTask> page = new Page<>(nursingTaskDto.getPageNum(), nursingTaskDto.getPageSize());
        queryWrapper.eq(NursingTask::getStatus, nursingTaskDto.getStatus());
        if (ObjUtil.isNotEmpty(nursingTaskDto.getNurseId())) {
            queryWrapper.eq(NursingTask::getNursingId, nursingTaskDto.getNurseId());
        }
        if (ObjUtil.isNotEmpty(nursingTaskDto.getElderName())) {
            queryWrapper.like(NursingTask::getElderName, nursingTaskDto.getElderName());
        }
        if (ObjUtil.isNotEmpty(nursingTaskDto.getProjectId())) {
            queryWrapper.eq(NursingTask::getProjectId, nursingTaskDto.getProjectId());
        }
        if (ObjUtil.isNotEmpty(nursingTaskDto.getStartTime()) && ObjUtil.isNotEmpty(nursingTaskDto.getEndTime())) {
            queryWrapper.between(NursingTask::getEstimatedServerTime, nursingTaskDto.getStartTime(), nursingTaskDto.getEndTime());
        }
        //List<NursingTask> list = (List<NursingTask>) queryWrapper;
        //执行分页
        page = page(page, queryWrapper);

        return new TableDataInfo<>(page.getRecords(), (int) page.getTotal());
    }
}
